def get_prev_item(objects, fieldname, num):
    try:
        kwargs = {fieldname + '__lt': num}
        return objects.filter(**kwargs).order_by('-' + fieldname)[0]
    except IndexError:
        return None

def get_next_item(objects, fieldname, num):
    try:
        kwargs = {fieldname + '__gt': num}
        return objects.filter(**kwargs).order_by(fieldname)[0]
    except IndexError:
        return None

def get_min(objects, fieldname):
    try:
        return int(getattr(objects.order_by(fieldname)[0], fieldname))
    except (IndexError, ValueError):
        return 0

def get_max(objects, fieldname):
    try:
        return int(getattr(objects.order_by('-' + fieldname)[0], fieldname))
    except (IndexError, ValueError):
        return 0
